/*
 * @Author: zxh
 * @Email: 1271027008@qq.com
 * @Date: 2020-03-12 14:05:17
 * @Descripttion: 物料有效期定义
 */

import { Component, OnInit } from '@angular/core';
import { MaterialValidityService } from './material-validity.service'
import { HCommonService } from 'src/app/h-common/h-common.service';
import { NzModalService } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-material-validity',
  templateUrl: './material-validity.component.html',
  styleUrls: ['./material-validity.component.scss']
})
export class MaterialValidityComponent implements OnInit {

  constructor(
    private MaterialValidityService: MaterialValidityService,
    private HCommonService: HCommonService,
    private NzModalService: NzModalService,
    private NzMessageService: NzMessageService,
  ) { }

  ngOnInit() {
    /* 获取权限 */
    //this.power = this.HCommonService.getRoles("validityConfig");
    this.getMaterValiList()
    this.getMaterialTyep()
  }
  power: any//权限

  pageSize: number = 10
  materialCode: string = ''
  materialName: string = ''
  materialType: string = ''
  materialTypeList = []
  selectMaterialType(typeId) {
    this.materialType = typeId
    this.getMaterValiList('search')
  }
  getMaterialTyep() {
    let _data = {
      dictName: 'material_category'
    }
    this.MaterialValidityService.getMaterialList(_data).subscribe(res => {
      this.materialTypeList = res
    })
  }
  // table
  pageIndex: number = 1
  totalSize: number
  clickPage(index) {
    this.pageIndex = index
    this.getMaterValiList()
  }

  isAllDisplayDataChecked = false;
  isIndeterminate = false;
  listOfDisplayData = [];
  listOfAllData = [];
  mapOfCheckedId: { [key: string]: boolean } = {};
  numberOfChecked = 0;
  checkedId = []

  currentPageDataChange($event): void {
    this.listOfDisplayData = $event;
    this.refreshStatus();
  }
  refreshStatus(): void {
    //是否是全选中状态  
    this.isAllDisplayDataChecked = this.listOfDisplayData
      .every(item => this.mapOfCheckedId[item.id]);
    if (this.listOfAllData.length == 0) {
      this.isAllDisplayDataChecked = false
    }
    //是否是半选中状态  
    this.isIndeterminate =
      this.listOfDisplayData.some(item => this.mapOfCheckedId[item.id]) &&
      !this.isAllDisplayDataChecked;
    //选中数量
    this.numberOfChecked = this.listOfAllData.filter(item => this.mapOfCheckedId[item.id]).length;
    //选中条
    let checkedList = this.listOfAllData.filter(item => this.mapOfCheckedId[item.id])
    let newIds = []
    checkedList.forEach(item => {
      newIds.push(item.id)
    })
    this.checkedId = newIds
  }
  checkAll(value: boolean): void {
    this.listOfDisplayData.forEach(item => (this.mapOfCheckedId[item.id] = value));
    this.refreshStatus();
  }
  //table

  // 获取物料有效期定义列表
  getMaterValiList(type?) {
    if (type) {
      this.pageIndex = 1
    }
    let _data = {
      code: this.materialCode,
      name: this.materialName,
      category: this.materialType,
      page: this.pageIndex - 1,
      size: this.pageSize
    }
    this.MaterialValidityService.getValidityList(_data).subscribe(res => {
      res.content.forEach(item => {
        item.checkDuration = item.validityConfig ? item.validityConfig.checkDuration : ''
        item.checkCycle = item.validityConfig ? item.validityConfig.checkCycle : ''
        item.isRecheck = item.validityConfig ? item.validityConfig.isRecheck : false
        item.remindDay = item.validityConfig ? item.validityConfig.remindDay : ''
      })
      this.listOfAllData = res.content
      this.totalSize = res.totalElements
    })
  }
  // 新增
  addEditMaterVali() {
    let checkedList = this.listOfAllData.filter(item => this.mapOfCheckedId[item.id])[0]
    let title = '确定保存吗？'
    if (checkedList.validityConfig) {
      title = '确定修改吗？'
    }
    this.NzModalService.confirm({
      nzTitle: title,
      nzOnOk: () => {
        if (Number(checkedList.checkCycle) <= Number(checkedList.remindDay)) {
          this.NzMessageService.create('error', '复检周期必须大于提醒周期!')
        } else {
          let _data = {
            id: checkedList.id,
            checkCycle: Number(checkedList.checkCycle),
            checkDuration: Number(checkedList.checkDuration),
            isRecheck: checkedList.isRecheck,
            remindDay: Number(checkedList.remindDay),
            validityDayNum: Number(checkedList.validDay)
          }
          if (checkedList.validityConfig) {
            this.MaterialValidityService.editValidity(_data).subscribe(res => {
              console.log(res)
              this.checkedId = []
              this.mapOfCheckedId = {}
              this.getMaterValiList()
            })
          } else {
            this.MaterialValidityService.addValidity(_data).subscribe(res => {
              console.log(res)
              this.checkedId = []
              this.mapOfCheckedId = {}
              this.getMaterValiList()
            })
          }
        }
      }
    })
  }

}
